#include "filesys.h"
#include "system.h"
#include <fstream>

namespace sys
{
	 const char DELIMETER = '\\';
	void FilePath::RemoveLastPart()
	{
  		size_type pos = find_last_of(DELIMETER);
  		if (pos != npos)
			resize(pos);
	}
	
	void FilePath::AssignToCurrentPath()
	{
		const DWORD size = 1024;
		char buffer[size];
		GetModuleFileName(
			  NULL,
			  buffer,
		  	  size);
		assign(buffer);

	}
	
 	File::File(const string& filePath)
  	{
  		_filePath = filePath;
  	}
  	
  	void File::Backup()
	{
    	std::ifstream from(_filePath.c_str());
    	if (!from)
    		throw runtime_error_ex("Could not open file '%s' for reading",
    				_filePath.c_str());
    	const string toPath = _filePath + "~";
    	std::ofstream to(toPath.c_str());
    	if (!to)
    		throw runtime_error_ex("Could not open file '%s' for writing",
    			toPath.c_str());
    	char ch;
    	while (from.get(ch)) to.put(ch);
    	if (!from.eof() || !to)
    		throw runtime_error_ex("Copy of '%s' not successfull",
		       _filePath.c_str());
  	}
}
